﻿<common:LayoutAwarePage
    x:Name="pageRoot"
    x:Class="Expenses.Views.ChargesPage"
    DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
    IsTabStop="false"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Expenses.Views"
    xmlns:common="using:Expenses.Common"
    xmlns:converters="using:Expenses.Converters"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Page.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Styles/ExpensesStyles.xaml"/>
            </ResourceDictionary.MergedDictionaries>

            <CollectionViewSource x:Name="ChargesViewSource"/>

            <converters:DateTimeToStringConverter x:Key="DateTimeToStringConverter"/>
            <converters:DateTimeToDaysOldStringConverter x:Key="DateTimeToDaysOldStringConverter"/>
            <converters:DoubleToCurrencyStringConverter x:Key="DoubleToCurrencyStringConverter"/>

            <DataTemplate x:Key="ChargeTemplate">
                <Grid Background="{StaticResource ItemTemplateBackgroundColor}"
                      Height="150">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="125"/>
                        <ColumnDefinition Width="200"/>
                    </Grid.ColumnDefinitions>
                    <Grid Grid.Column="0" 
                          Margin="10,5,0,5">
                        <StackPanel HorizontalAlignment="Left" 
                                    VerticalAlignment="Top">
                            <TextBlock FontSize="36" 
                                       FontWeight="Bold" 
                                       Text="{Binding ExpenseDate, Converter={StaticResource DateTimeToDaysOldStringConverter}}" 
                                       FontFamily="{StaticResource FontFamilyTitle}"
                                       HorizontalAlignment="Left" 
                                       VerticalAlignment="Top" 
                                       Margin="0,0,0,0" />
                            <TextBlock FontSize="18" 
                                       Text="day(s) old" 
                                       HorizontalAlignment="Left" 
                                       VerticalAlignment="Top" />
                        </StackPanel>
                    </Grid>
                    <Grid Grid.Column="1" 
                          Margin="0,5,10,10">
                        <StackPanel HorizontalAlignment="Left" 
                                    VerticalAlignment="Top">
                            <TextBlock Text="{Binding ExpenseDate, Converter={StaticResource DateTimeToStringConverter}}" 
                                       FontSize="20" 
                                       TextWrapping="Wrap"
                                       HorizontalAlignment="Left" 
                                       VerticalAlignment="Center" />
                            <TextBlock Text="{Binding Merchant}" 
                                       FontSize="20" 
                                       TextWrapping="Wrap"
                                       HorizontalAlignment="Left" 
                                       VerticalAlignment="Center" />
                            <TextBlock Text="{Binding Location}" 
                                       FontSize="20" 
                                       TextWrapping="Wrap"
                                       HorizontalAlignment="Left" 
                                       VerticalAlignment="Center" />
                            <TextBlock Text="{Binding BilledAmount, Converter={StaticResource DoubleToCurrencyStringConverter}}" 
                                       FontSize="22" 
                                       FontWeight="Bold" 
                                       Margin="0,10,0,0"
                                       HorizontalAlignment="Left" 
                                       VerticalAlignment="Center" />
                        </StackPanel>
                    </Grid>
                </Grid>
            </DataTemplate>

            <DataTemplate x:Key="SnappedChargeTemplate">
                <Grid Background="{StaticResource ItemTemplateBackgroundColor}"
                      Height="150">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="100"/>
                        <ColumnDefinition Width="160"/>
                    </Grid.ColumnDefinitions>
                    <Grid Grid.Column="0" 
                          Margin="10,5,0,5">
                        <StackPanel HorizontalAlignment="Left" 
                                    VerticalAlignment="Top">
                            <TextBlock FontSize="28" 
                                       FontWeight="Bold" 
                                       Text="{Binding ExpenseDate, Converter={StaticResource DateTimeToDaysOldStringConverter}}" 
                                       FontFamily="{StaticResource FontFamilyTitle}"
                                       HorizontalAlignment="Left" 
                                       VerticalAlignment="Top" 
                                       Margin="0,0,0,0" />
                            <TextBlock FontSize="14" 
                                       Text="day(s) old" 
                                       HorizontalAlignment="Left" 
                                       VerticalAlignment="Top" />
                        </StackPanel>
                    </Grid>
                    <Grid Grid.Column="1" 
                          Margin="0,5,10,10">
                        <StackPanel HorizontalAlignment="Left" 
                                    VerticalAlignment="Top">
                            <TextBlock Text="{Binding ExpenseDate, Converter={StaticResource DateTimeToStringConverter}}" 
                                       FontSize="16" 
                                       TextWrapping="Wrap"
                                       HorizontalAlignment="Left" 
                                       VerticalAlignment="Center" />
                            <TextBlock Text="{Binding Merchant}" 
                                       FontSize="16" 
                                       TextWrapping="Wrap"
                                       HorizontalAlignment="Left" 
                                       VerticalAlignment="Center" />
                            <TextBlock Text="{Binding Location}" 
                                       FontSize="16" 
                                       TextWrapping="Wrap"
                                       HorizontalAlignment="Left" 
                                       VerticalAlignment="Center" />
                            <TextBlock Text="{Binding BilledAmount, Converter={StaticResource DoubleToCurrencyStringConverter}}" 
                                       FontSize="18" 
                                       FontWeight="Bold" 
                                       Margin="0,10,0,0"
                                       HorizontalAlignment="Left" 
                                       VerticalAlignment="Center" />
                        </StackPanel>
                    </Grid>
                </Grid>
            </DataTemplate>

        </ResourceDictionary>
    </Page.Resources>

    <Page.BottomAppBar>
        <AppBar x:Name="ChargesAppBar"  
                IsEnabled="{Binding IsUIEnabled}"
                Background="{StaticResource ItemTemplateBackgroundColor}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="50*"/>
                    <ColumnDefinition Width="50*"/>
                </Grid.ColumnDefinitions>
                <StackPanel Grid.Column="0"
                            Orientation="Horizontal" 
                            HorizontalAlignment="Left" 
                            VerticalAlignment="Top">
                    <Button x:Name="ClearSelectionButton" 
                            Style="{StaticResource ClearSelectionAppBarButtonStyle}" 
                            Loaded="StartLayoutUpdates"
                            Unloaded="StopLayoutUpdates"
                            Click="ClearSelectionButton_Click"
                            Visibility="Collapsed"/>
                    <Button x:Name="AddToReportButton" 
                            Style="{StaticResource AddToReportAppBarButtonStyle}" 
                            Loaded="StartLayoutUpdates"
                            Unloaded="StopLayoutUpdates"
                            Click="AddToReportButton_Click"
                            Visibility="Collapsed"/>
                </StackPanel>
                <StackPanel Grid.Column="1"
                            Orientation="Horizontal" 
                            HorizontalAlignment="Right" 
                            VerticalAlignment="Top">
                    <Button x:Name="SelectAllButton" 
                            Style="{StaticResource SelectAllAppBarButtonStyle}" 
                            Loaded="StartLayoutUpdates"
                            Unloaded="StopLayoutUpdates"
                            Click="SelectAllButton_Click"/>
                    <Button x:Name="NewChargeButton" 
                            Style="{StaticResource NewChargeAppBarButtonStyle}" 
                            Loaded="StartLayoutUpdates"
                            Unloaded="StopLayoutUpdates"
                            Click="NewChargeButton_Click"/>
                </StackPanel>
            </Grid>
        </AppBar>
    </Page.BottomAppBar>

    <Grid x:Name="LayoutRoot" 
          Background="{StaticResource AppBackgroundColor}">
        <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Border Background="{StaticResource ItemTemplateBackgroundColor}">
                <Button x:Name="BackButton" 
                        Click="BackButton_Click" 
                        Style="{StaticResource BackButtonStyle}"/>
            </Border>
            <Border Background="{StaticResource ItemTemplateBackgroundColor}"
                    Grid.Column="1">
                <TextBlock x:Name="PageTitleTextBlock" 
                           Text="Charges"
                           Style="{StaticResource PageHeaderTextStyle}"
                           TextWrapping="Wrap"
                           FontSize="44"/>
            </Border>
        </Grid>

        <ProgressRing x:Name="ChargesProgressRing" 
                      Grid.Row="1" 
                      Height="150"
                      Width="150"
                      IsActive="False"/>

        <GridView x:Name="ChargesGridView"
                  Grid.Row="1"
                  Margin="120,50,0,0"
                  ItemsSource="{Binding Source={StaticResource ChargesViewSource}}"
                  ItemTemplate="{StaticResource ChargeTemplate}"
                  SelectionMode="Multiple"
                  IsSwipeEnabled="True"
                  IsItemClickEnabled="True"
                  ItemClick="ChargesGridView_ItemClick"
                  SelectionChanged="ChargesGridView_SelectionChanged">
            <GridView.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapGrid Orientation="Vertical"/>
                </ItemsPanelTemplate>
            </GridView.ItemsPanel>
        </GridView>

        <ListView x:Name="ChargesListView"
                  AutomationProperties.AutomationId="ChargesListView"
                  AutomationProperties.Name="Grouped Items"
                  Grid.Row="1"
                  Width="300"
                  Visibility="Collapsed"
                  Margin="15,0,0,0"
                  ItemsSource="{Binding Source={StaticResource ChargesViewSource}}"
                  ItemTemplate="{StaticResource SnappedChargeTemplate}"
                  SelectionMode="Multiple"
                  IsSwipeEnabled="True"
                  IsItemClickEnabled="True"
                  ItemClick="ChargesGridView_ItemClick"
                  SelectionChanged="ChargesGridView_SelectionChanged">
        </ListView>

        <VisualStateManager.VisualStateGroups>

            <!-- Visual states reflect the application's view state -->
            <VisualStateGroup x:Name="ApplicationViewStates">
                <VisualState x:Name="FullScreenLandscape"/>
                <VisualState x:Name="Filled"/>

                <!-- The entire page respects the narrower 100-pixel margin convention for portrait -->
                <VisualState x:Name="FullScreenPortrait">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BackButton" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PortraitBackButtonStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PageTitleStackPanel" 
                                                       Storyboard.TargetProperty="Orientation">
                            <DiscreteObjectKeyFrame KeyTime="0" 
                                                    Value="Vertical"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PageTitleStackPanel" 
                                                       Storyboard.TargetProperty="Margin">
                            <DiscreteObjectKeyFrame KeyTime="0" 
                                                    Value="20,0,0,0"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PageTitleTextBlock" 
                                                       Storyboard.TargetProperty="Margin">
                            <DiscreteObjectKeyFrame KeyTime="0" 
                                                    Value="0,20,0,0"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ChargesSummaryTextBlock" 
                                                       Storyboard.TargetProperty="Margin">
                            <DiscreteObjectKeyFrame KeyTime="0" 
                                                    Value="0,20,0,10"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>

                <!-- The back button and title have different styles when snapped -->
                <VisualState x:Name="Snapped">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BackButton" 
                                                       Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" 
                                                    Value="{StaticResource SnappedBackButtonStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PageTitleTextBlock" 
                                                       Storyboard.TargetProperty="FontSize">
                            <DiscreteObjectKeyFrame KeyTime="0" 
                                                    Value="20"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PageTitleTextBlock" 
                                                       Storyboard.TargetProperty="FontWeight">
                            <DiscreteObjectKeyFrame KeyTime="0" 
                                                    Value="SemiBold"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ChargesGridView" 
                                                       Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0" 
                                                    Value="Collapsed"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ChargesListView" 
                                                       Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0" 
                                                    Value="Visible"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ChargesListView" 
                                                       Storyboard.TargetProperty="Margin">
                            <DiscreteObjectKeyFrame KeyTime="0" 
                                                    Value="15,20,0,0"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Grid>
</common:LayoutAwarePage>
